﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Sedge.Core.Globalization;
using Sedge.UI.Controller;
using Sedge.UI.ViewModel;

namespace Sedge.UI.View
{
	public partial class ErrorDetailsView : UserControl, ITransferable
	{
		#region Private Fields
		
		private readonly ErrorDetailsViewModel _viewModel;
		
		#endregion //Private Fields

		#region Constructors

		public ErrorDetailsView()
		{
			InitializeComponent();
		}

		public ErrorDetailsView(ErrorDetailsViewModel viewModel)
			: this()
		{
			_viewModel = viewModel;
			SetupWindow();
		}

		#endregion //Constructors

		#region Private Methods
		
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			labelExample.Left = textDescription.Right - labelExample.Width;
			labelSeverity.Left = comboSeverity.Left - labelSeverity.Width;
			int left = Math.Max(labelAppName.Right, labelType.Right);
			comboApp.Left = left;
			comboApp.Width = labelSeverity.Left - left - 20;
			comboType.Left = left;
		}

		private void SetupWindow()
		{
			labelAppName.Text = Locale.Strings.DetailsApp;
			labelSeverity.Text = Locale.Strings.DetailsSeverity;
			labelType.Text = Locale.Strings.DetailsReportType;
			labelDescription.Text = Locale.Strings.DetailsDescription;
			
			labelExample.Text = Locale.Strings.DetailsExample;
			labelExample.Visible = _viewModel.CanShowExaple();

			comboSeverity.Items.AddRange(_viewModel.GetSeverityLevels());
			comboType.Items.AddRange(_viewModel.GetReportTypes());

			string selectedApp;
			List<string> appNames = _viewModel.GetApplications(out selectedApp);
			if (appNames.Count != 0)
			{
				comboApp.Items.AddRange(appNames.ToArray());
				if (appNames.Count == 1)
				{
					comboApp.SelectedIndex = 0;
					comboApp.Enabled = false;
				}
				else
					comboApp.Text = selectedApp;
			}
			else
			{
				comboApp.DropDownStyle = ComboBoxStyle.Simple;
			}
		}

		#endregion Private Methods

		private void labelExample_Click(object sender, EventArgs e)
		{
			_viewModel.ShowExample();
		}

		#region Public Methods
		
		public void TransferData()
		{
			_viewModel.StoreData(comboApp.Text, comboSeverity.SelectedIndex, 
										comboType.SelectedIndex, textDescription.Lines);
		}
		
		#endregion //Public Methods
	}
}
